*Purpose: Summary Statistic Tables for matching, pretreatment t-statistics, and other

clear

**1. Packages
ssc install dataex, replace
ssc install estout, replace
net install grc1leg,from( http://www.stata.com/users/vwiggins/)

** 2. Cleaning up some variables
capture program drop names
program names 
	rename NearCity_dist_km 		city_dist
end

*_______________________________________________________________________________
** 3. Write programs to extract statistics from time-variant and time-invariant variables

**TOP VARIABLE (Time Variant)
capture program drop means
program means
estpost sum `1' if treat == 0 & post  == 0
estimates store col1_`1'
estpost sum `1' if treat == 1 & post  == 0
estimates store col2_`1'
estpost sum `1' if treat == 0 & post  == 1
estimates store col3_`1'
estpost sum `1' if treat == 1 & post  == 1
estimates store col4_`1'


foreach num in 0 1 {
	
ttest `1' if post == `num', by(treat)
preserve
replace `1' = (r(mu_2) - r(mu_1)) / sqrt( ( r(sd_2)^2 + r(sd_1)^2 )  / 2 )
estpost sum `1' 
restore
estimates store norm_diff_`1'_`num'
ttest `1' if post == `num', by(treat)
preserve
replace `1' = (r(sd_2)^2)/(r(sd_1)^2)
estpost sum `1'
restore
estimates store varrat_`1'_`num'
}

end

**MIDDLE VARIABLES
*Time-Variant (Middle Variables)
capture program drop means_timevariant
program means_timevariant
estpost sum `1' if treat == 0 & post  == 0
estimates store col1_`1'
estpost sum `1' if treat == 1 & post  == 0
estimates store col2_`1'
estpost sum `1' if treat == 0 & post  == 1
estimates store col3_`1'
estpost sum `1' if treat == 1 & post  == 1
estimates store col4_`1'


foreach num in 0 1 {
	
ttest `1' if post == `num', by(treat)
preserve
replace `1' = (r(mu_2) - r(mu_1)) / sqrt( ( r(sd_2)^2 + r(sd_1)^2 )  / 2 )
estpost sum `1' 
restore
estimates store norm_diff_`1'_`num'
ttest `1' if post == `num', by(treat)
preserve
replace `1' = (r(sd_2)^2)/(r(sd_1)^2)
estpost sum `1'
restore
estimates store varrat_`1'_`num'
}

label var `1' "`2'"
esttab col1_`1' col2_`1' norm_diff_`1'_0  varrat_`1'_0 using "${tables}\Balance_`4'.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs

label var `1' "`3'"
esttab col3_`1' col4_`1' norm_diff_`1'_1 varrat_`1'_1 using "${tables}\Balance_`4'.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs

end

*Time-invariant (Middle Variables) 
capture program drop means_timeinvariant
program means_timeinvariant
estpost sum `1' if treat == 0 & post  == 0
estimates store col1_`1'
estpost sum `1' if treat == 1 & post  == 0
estimates store col2_`1'
estpost sum `1' if treat == 0 & post  == 1
estimates store col3_`1'
estpost sum `1' if treat == 1 & post  == 1
estimates store col4_`1'


foreach num in 0 1 {
	
ttest `1' if post == `num', by(treat)
preserve
replace `1' = (r(mu_2) - r(mu_1))/(sqrt(r(sd_2)^2 + r(sd_1)^2))
estpost sum `1' 
restore
estimates store norm_diff_`1'_`num'
ttest `1' if post == `num', by(treat)
preserve
replace `1' = (r(sd_2)^2)/(r(sd_1)^2)
estpost sum `1'
restore
estimates store varrat_`1'_`num'
}

label var `1' "`2'"
esttab col1_`1' col2_`1' norm_diff_`1'_0  varrat_`1'_0 using "${tables}\Balance_`3'.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs

end

if 0{
*_______________________________________________________________________________
** 3. Pre-Matching Table
use "$data\MASTER_Grids_Period_R2.dta", clear //MUST UPDATE THIS SO THAT THERE IS NO 2019 DATA

**Clean 
names
gen PA_relevant = PA_status_yr < = 2000

**Top Variable  // 

quietly {
means Net_Recover 
means DEF_Recover 
means DEF_Loss
}

label var Net_Recover "Net forest recovery (ha), 2010-2018"
la var DEF_Recover "Forest recovery (ha), 2010-2018"
la var DEF_Loss "Forest loss (ha), 2010-2018"

esttab col3_Net_Recover col4_Net_Recover norm_diff_Net_Recover_1 varrat_Net_Recover_1  using "${tables}\Balance_Prematch.tex",  prehead("& Untreated & Treated & Norm. diff & Var ratio \\ \hline \\ Post-treatment & \\ ") replace fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs

esttab col3_DEF_Recover col4_DEF_Recover norm_diff_DEF_Recover_1 varrat_DEF_Recover_1 using "${tables}\Balance_Prematch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 

esttab col3_DEF_Loss col4_DEF_Loss norm_diff_DEF_Loss_1 varrat_DEF_Loss_1 using "${tables}\Balance_Prematch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs postfoot("\\ Pre-treatment & \\")

label var Net_Recover "Net forest recovery (ha), 2000-2009" 
la var DEF_Recover "Forest recovery (ha), 2000-2009"
la var DEF_Loss "Forest loss (ha), 2000-2009"

foreach m in Net_Recover DEF_Recover DEF_Loss {
esttab col1_`m' col2_`m' norm_diff_`m'_0 varrat_`m'_0 using "${tables}\Balance_Prematch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 
}

**Middle Variables
means_timevariant COV_FOREST 	"Forest cover (ha), 2000" 					"Forest cover (ha), 2010"					"Prematch"
means_timevariant COV_NONFOREST	"Nonforest cover (ha), 2000" 				"Nonforest cover (ha), 2010"				"Prematch"

means_timeinvariant elevation 	"Elevation (m)"		"Prematch"
means_timeinvariant slope 		"Slope (deg)"		"Prematch"
means_timeinvariant PA_relevant		"Protected area"	"Prematch"  // changed this variable from PA_relevant, which is not in dataset

**Bottom Variable
means city_dist
label var city_dist "Distance to City (km)" 
esttab col1_city_dist col2_city_dist norm_diff_city_dist_0 varrat_city_dist_0 using "${tables}\Balance_Prematch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers

}

if 0{
*_______________________________________________________________________________
** 4.1 PSM Post-Matching Tables
use "$data\MASTER_MATCHED_R2.dta", clear

**Clean Names
names

**Top Variable
quietly {
means Net_Recover 
means DEF_Recover 
means DEF_Loss
}

label var Net_Recover "Net forest recovery (ha), 2010-2018"
la var DEF_Recover "Forest recovery (ha), 2010-2018"
la var DEF_Loss "Forest loss (ha), 2010-2018"

esttab col3_Net_Recover col4_Net_Recover norm_diff_Net_Recover_1 varrat_Net_Recover_1  using "${tables}\Balance_Postmatch.tex",  prehead("& Untreated & Treated & Norm. diff & Var ratio \\ \hline \\ Post-treatment & \\ ") replace fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs

esttab col3_DEF_Recover col4_DEF_Recover norm_diff_DEF_Recover_1 varrat_DEF_Recover_1 using "${tables}\Balance_Postmatch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 

esttab col3_DEF_Loss col4_DEF_Loss norm_diff_DEF_Loss_1 varrat_DEF_Loss_1 using "${tables}\Balance_Postmatch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs postfoot("\\ Pre-treatment & \\")

label var Net_Recover "Net forest recovery (ha), 2000-2009" 
la var DEF_Recover "Forest recovery (ha), 2000-2009"
la var DEF_Loss "Forest loss (ha), 2000-2009"

foreach m in Net_Recover DEF_Recover DEF_Loss {
esttab col1_`m' col2_`m' norm_diff_`m'_0 varrat_`m'_0 using "${tables}\Balance_Postmatch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 
}


**Middle Variables
means_timevariant COV_FOREST 	"Forest cover (ha), 2000" 					"Forest cover (ha), 2010"					"Postmatch"
means_timevariant COV_NONFOREST	"Nonforest cover (ha), 2000" 				"Nonforest cover (ha), 2010"				"Postmatch"

means_timeinvariant elevation 	"Elevation (m)"		"Postmatch"
means_timeinvariant slope 		"Slope (deg)"		"Postmatch"
means_timeinvariant PA_relevant	"Protected area"	"Postmatch"

**Bottom Variable
means city_dist
label var city_dist "Distance to City (km)" 
esttab col1_city_dist col2_city_dist norm_diff_city_dist_0  varrat_city_dist_0 using "${tables}\Balance_Postmatch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers

}


if 0{
*_______________________________________________________________________________
** 4.2 CEM Post-Matching Tables
use "$data\CEM_k2k_MASTER_MATCHED_R2.dta", clear

**Clean Names
names

**Top Variable
quietly {
means Net_Recover 
means DEF_Recover 
means DEF_Loss
}

label var Net_Recover "Net forest recovery (ha), 2010-2018"
la var DEF_Recover "Forest recovery (ha), 2010-2018"
la var DEF_Loss "Forest loss (ha), 2010-2018"

esttab col3_Net_Recover col4_Net_Recover norm_diff_Net_Recover_1 varrat_Net_Recover_1  using "${tables}\Balance_CEM_Postmatch.tex",  prehead("& Untreated & Treated & Norm. diff & Var ratio \\ \hline \\ Post-treatment & \\ ") replace fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs

esttab col3_DEF_Recover col4_DEF_Recover norm_diff_DEF_Recover_1 varrat_DEF_Recover_1 using "${tables}\Balance_CEM_Postmatch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 

esttab col3_DEF_Loss col4_DEF_Loss norm_diff_DEF_Loss_1 varrat_DEF_Loss_1 using "${tables}\Balance_CEM_Postmatch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs postfoot("\\ Pre-treatment & \\")

label var Net_Recover "Net forest recovery (ha), 2000-2009" 
la var DEF_Recover "Forest recovery (ha), 2000-2009"
la var DEF_Loss "Forest loss (ha), 2000-2009"

foreach m in Net_Recover DEF_Recover DEF_Loss {
esttab col1_`m' col2_`m' norm_diff_`m'_0 varrat_`m'_0 using "${tables}\Balance_CEM_Postmatch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 
}

**Middle Variables
means_timevariant COV_FOREST 	"Forest cover (ha), 2000" 					"Forest cover (ha), 2010"					"CEM_Postmatch"
means_timevariant COV_NONFOREST	"Nonforest cover (ha), 2000" 				"Nonforest cover (ha), 2010"				"CEM_Postmatch"

means_timeinvariant elevation_avg 	"Elevation (m)"		"CEM_Postmatch"
means_timeinvariant slope_avg 		"Slope (deg)"		"CEM_Postmatch"
means_timeinvariant PA_relevant	"Protected area"	"CEM_Postmatch"

**Bottom Variable
means city_dist
label var city_dist "Distance to City (km)" 
esttab col1_city_dist col2_city_dist norm_diff_city_dist_0  varrat_city_dist_0 using "${tables}\Balance_CEM_Postmatch.tex", append fragment cells("mean(fmt(3))") label style (tex) collabels(none) nomtitle noline nonumbers

}

if 0{
*_______________________________________________________________________________
** 5. T-tests of pretreatment matched trends 
use "$data\MASTER_MATCHED_yearR2.dta", clear

capture drop NetOverNF

**Create Annual Proportional Outcome (NetOverNF)
edit state cell year post treat Net_Rec COV_NON
sort state2 cell year
	
	bysort state2 cell year: gen COV_NF_pre = COV_NONFOREST if year==2000
	replace COV_NF_pre = COV_NF_pre[_n-1] if year >2000 & year <2010
	
	bysort state2 cell year: gen COV_NF_post = COV_NONFOREST if year ==2010 
	replace COV_NF_post = COV_NF_post[_n-1] if year >2010 & year <2022
	
	gen NetOverNF = Net_Recover / COV_NF_pre if year >=2000 & year <2010
		replace NetOverNF = Net_Recover / COV_NF_post if year>=2010 & year <2022

**Run T-tests
foreach v in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 {
	estpost ttest Net_Recover if year==20`v', by(treat)
		est sto NR_ttest_20`v'
		
	estpost ttest NetOverNF if year==20`v', by(treat)
		est sto NONF_ttest_20`v'
}
 
**Net_Recover Table
*Top of Table
label var Net_Recover "2000" 
esttab NR_ttest_2000 using "${tables}\NR_Ttest_Pretrends.tex", prehead("& Untreated & Treated & Diff & s.e. & obs \\ \hline \\") replace fragment cells("mu_1(fmt(3)) mu_2  b(star) se(fmt(5)) count(fmt(0))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 

*Middle of Table
foreach v in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 {
label var Net_Recover "20`v'"
esttab NR_ttest_20`v' using "${tables}\NR_Ttest_Pretrends.tex", append fragment cells("mu_1(fmt(3)) mu_2  b(star) se(fmt(5)) count(fmt(0))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 
}

*Bottom of Table 
label var Net_Recover "2018" 
esttab NR_ttest_2018 using "${tables}\NR_Ttest_Pretrends.tex", append fragment cells("mu_1(fmt(3)) mu_2  b(star) se(fmt(5)) count(fmt(0))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 


**NetOverNF Table 
*Top of Table
label var NetOverNF "2000" 
esttab NONF_ttest_2000 using "${tables}\NONF_Ttest_Pretrends.tex", prehead("& Untreated & Treated & Diff & s.e. & obs \\ \hline \\") replace fragment cells("mu_1(fmt(3)) mu_2  b(star) se(fmt(5)) count(fmt(0))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 

*Middle of Table
foreach v in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 {
label var NetOverNF "20`v'"
esttab NONF_ttest_20`v' using "${tables}\NONF_Ttest_Pretrends.tex", append fragment cells("mu_1(fmt(3)) mu_2  b(star) se(fmt(5)) count(fmt(0))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 
}

*Bottom of Table 
label var NetOverNF "2018" 
esttab NONF_ttest_2018 using "${tables}\NONF_Ttest_Pretrends.tex", append fragment cells("mu_1(fmt(3)) mu_2  b(star) se(fmt(5)) count(fmt(0))") label style (tex) collabels(none) nomtitle noline nonumbers noobs 

}

if 1{
*_______________________________________________________________________________
** 6. Parallel Trends Graph 
use "$data\MASTER_MATCHED_yearR2.dta", clear

preserve
collapse (mean) Net_R, by(year treat)
tw (line Net_R year if treat==1, color("51 166 166") lwidth(0.5)) (line Net_R year if treat==0, color("242 80 65") lwidth(0.5)), xline(2009) legend(lab (1 "Treated") lab(2 "Untreated") pos(6) col(2)) ytitle("Net Forest Recovery (ha)") xlabel(2000(2)2018)
graph export "${figures}\NF_line_ParallelTrends.eps", replace
graph export "${figures}\NF_line_ParallelTrends.PNG", replace as(png)

tw (lowess Net_R year if treat==1) (lowess Net_R year if treat==0), xline(2009) legend(lab (1 "Treated") lab(2 "Untreated") pos(6) col(2)) ytitle("Net Forest Recovery (ha)") xlabel(2000(2)2018)
graph export "${figures}\NF_lowess_ParallelTrends.eps", replace
graph export "${figures}\NF_lowess_ParallelTrends.PNG", replace as(png)
restore 



*_______________________________________________________________________________
** 7. Regression to Check Parallel Trends

lab var year "Year"
gen year2 = year^2 
	lab var year2 "Year (Quad)"
gen yeartreat = year*treat
	lab var yeartreat "Year x Treat"
gen year2treat = year2*treat
	lab var year2treat "Year (Quad) x Treat"

reg Net_Recover year yeartreat if post==0, r 
	est sto prelinear
reg Net_Recover year year2 yeartreat year2treat if post==0, r 
	est sto prequad
	
estout prelinear prequad, cells(b(star fmt(6)) ) legend label keep(yeartreat year2treat) order(yeartreat year2treat)
	
esttab prelinear prequad using "${tables}\Parallel_Trends_Regress.tex", label replace  starlevels(* 0.10 ** 0.05 *** 0.01)  se nonotes ///
keep(yeartreat year2treat ) ///
order(yeartreat year2treat ) ///
varwidth(3)  style(tex) mtitles("Linear" "Quadratic") b(%12.7f) se(%12.7f) 
}

if 0{

*_______________________________________________________________________________
** 8. Land Cover Table POSTMATCHING
use "$data\MASTER_MATCHED_yearR2.dta", clear
collapse (sum) COV*, by(treat post)
gen treated = "Untreated" if treat==1
	replace treated = "Treated" if treat==0
	
*2000 Coverage
graph bar (sum) COV_1 COV_3 COV_4 COV_2 COV_5 if post==0, over(treated) scheme(white_tableau) ///
	bar(1, color("51 166 166")) bar(2, color("242 149 68")) bar(3, color("140 31 102")) bar(4, color("166 94 51")) bar(5, color("50 54 115")) /// 
	legend(position(3) label(1 "Forest") label(2 "Farming") label(3 "Non-vegetated area") label(4 "Non-forest natural formation") label(5 "Water")) ///
	percentages ytitle("Percent Coverage in 2000") ylabel(0(20)80)
graph save "${figures}\Coverage_2000.gph", replace
graph export "${figures}\Coverage_2000.eps", replace
graph export "${figures}\Coverage_2000.PNG", replace as(png)

*2010 Coverage
graph bar (sum) COV_1 COV_3 COV_4 COV_2 COV_5 if post==1, over(treated) scheme(white_tableau) ///
	bar(1, color("51 166 166")) bar(2, color("242 149 68")) bar(3, color("140 31 102")) bar(4, color("166 94 51")) bar(5, color("50 54 115")) /// 
	legend(position(3) label(1 "Forest") label(2 "Farming") label(3 "Non-vegetated area") label(4 "Non-forest natural formation") label(5 "Water")) ///
	percentages ytitle("Percent Coverage in 2010") ylabel(0(20)80)
graph save "${figures}\Coverage_2010.gph", replace
graph export "${figures}\Coverage_2010.eps", replace
graph export "${figures}\Coverage_2010.PNG", replace as(png)

}


if 0{
*_______________________________________________________________________________
** 8. Land Cover Table PREMATCHING
use "$data\MASTER_Grids_Period_R2.dta" , clear // NEED THE VERSION OF THIS WITHOUT 2019 DATA


foreach v in COV_FOREST COV_NONFOREST COV_Total COV_1 COV_2 COV_3 COV_4 COV_5 {
	replace `v' = 2 if `v' > 2
	replace `v' = `v'/2
}
collapse (mean) COV*, by(treat post)
gen treated = "Untreated" if treat==1
	replace treated = "Treated" if treat==0
	
	
*2000 Coverage
graph bar (sum) COV_1 COV_3 COV_4 COV_2 COV_5 if post==0, over(treated) scheme(white_tableau) ///
	bar(1, color("51 166 166")) bar(2, color("242 149 68")) bar(3, color("140 31 102")) bar(4, color("166 94 51")) bar(5, color("50 54 115")) /// 
	legend(position(6) label(1 "Forest") label(2 "Farming") label(3 "Non-vegetated area") label(4 "Non-forest natural formation") label(5 "Water")) ///
	  percentages ytitle("Percent Coverage in 2000") ylabel(0(20)80)
graph save "${figures}\PreMatchCoverage2000.gph", replace
graph export "${figures}\PreMatchCoverage2000.eps", replace


*2010 Coverage
graph bar (sum) COV_1 COV_3 COV_4 COV_2 COV_5 if post==1, over(treated) scheme(white_tableau) ///
	bar(1, color("51 166 166")) bar(2, color("242 149 68")) bar(3, color("140 31 102")) bar(4, color("166 94 51")) bar(5, color("50 54 115")) /// 
		legend(position(6) label(1 "Forest") label(2 "Farming") label(3 "Non-vegetated area") label(4 "Non-forest natural formation") label(5 "Water")) ///
	percentages ytitle("Percent Coverage in 2010") ylabel(0(20)80)
graph save "${figures}\PreMatchCoverage2010.gph", replace
graph export "${figures}\PreMatchCoverage2010.eps", replace


*Graph Combine
* Combine Two Unmatched Graphs
grc1leg "${figures}\PreMatchCoverage2000.gph" "${figures}\PreMatchCoverage2010.gph", ///
    legendfrom("${figures}\PreMatchCoverage2000.gph") ///
    title("Unmatched") ///
    col(2)

graph export "${figures}\Coverage_Comparison_Unmatched.eps", replace
graph export "${figures}\Coverage_Comparison_Unmatched.PNG", replace as(png)

	
* Combine Four Graphs: PreMatch(2000,2010) + Coverage(2000,2010)
grc1leg "${figures}\PreMatchCoverage2000.gph" "${figures9}\Coverage_2000.gph" ///
        "${figures}\PreMatchCoverage2010.gph" "${figures9}\Coverage_2010.gph", ///
    legendfrom("${figures}\PreMatchCoverage2000.gph") ///
    title("Unmatched                                         Matched") ///
    col(4)

graph export "${figures}\Coverage_Comparison_All.eps", replace
graph export "${figures}\Coverage_Comparison_All.PNG", replace as(png)


}

